Skip to content

fix(v2): preserve nested struct JSON structure for embedded types with json tags#5213

Closed
leaanthony wants to merge 1 commit into
masterfrom
fix/4117-nested-struct-ts-bindings
Closed

fix(v2): preserve nested struct JSON structure for embedded types with json tags#5213
leaanthony wants to merge 1 commit into
masterfrom
fix/4117-nested-struct-ts-bindings

Conversation

@leaanthony

Copy link
Copy Markdown
Member

Summary

Fixes #4117

When a Go struct has anonymous embedded struct fields with explicit json tags (e.g., TimeLimitDef \json:"timeLimitDef"`), Go's encoding/json` marshals them as nested objects. However, the TS binding generator unconditionally flattened these fields, producing incorrect TypeScript that couldn't deserialize the JSON.

Changes

  • deepFields() in typescriptify.go: Check for json tags on anonymous fields before flattening. If an anonymous field has an explicit json tag name, treat it as a regular struct field.
  • AddStructToGenerateTS() in binding.go: Register embedded struct types with json tags for separate TS class generation instead of skipping them.
  • New test binding_embedded_named_struct_jsontag_test.go that reproduces the exact scenario from the bug report.

Before (buggy)

export class LicenseCreateEntity {
    name: string;
    typ: string;
    desc: string;
    isInfinite: boolean;    // Flattened!
    startTime: number;      // Flattened!
    endTime: number;        // Flattened!
    appName: string;        // Flattened!
    ...
}

After (fixed)

export class LicenseCreateEntity {
    name: string;
    typ: string;
    desc: string;
    timeLimitDef: TimeLimitDef;   // Nested!
    application: Application;      // Nested!
}

Test plan

  • All existing binding tests pass (no regressions)
  • New test EmbeddedNamedStructJSONTagTest passes with correct nested output
  • Verify generated TS bindings work with actual Go JSON serialization

…h json tags

When a Go struct has anonymous embedded struct fields with explicit json
tags (e.g., `TimeLimitDef `json:"timeLimitDef"``), Go's encoding/json
marshals them as nested objects. However, the TS binding generator
unconditionally flattened these fields, producing incorrect TypeScript
that couldn't deserialize the JSON.

Fix deepFields() in typescriptify to check for json tags on anonymous
fields before flattening. Fix AddStructToGenerateTS() in binding.go to
register embedded struct types with json tags for separate TS class
generation.

Fixes #4117
@coderabbitai

coderabbitai Bot commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bf1dccb5-34f9-4365-841d-5e99e931442e

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix/4117-nested-struct-ts-bindings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@leaanthony

Copy link
Copy Markdown
Member Author

🤖 PR Triage Review

Accepted

Preserves nested struct JSON structure for embedded types with json tags (v2 bindings). Improves JSON handling.

Platform: All (v2, bindings)

Next Steps: Dispatching for testing on all platforms.


Reviewed by Wails PR Reviewer Bot

@leaanthony leaanthony modified the milestones: v2.5.0, v2.13.0 May 20, 2026
@taliesin-ai

Copy link
Copy Markdown
Collaborator

Duplicate of #5221 — same embedded-struct JSON/TS generation fix; consolidating on #5221. Closing this one and keeping #5221.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[v2.9.0] generate module ts will error if go struct with nest sub struct,with named json tag

2 participants